package com.imonsoft.sqm.service.business.supplier_quality;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.google.gson.reflect.TypeToken;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.dao.business.supplier_quality.EnvManageDAO;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManage;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManageExample;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.validator.business.supplier_quality.EnvManageValidator;
import com.imonsoft.sqm.vo.business.supplier_quality.EnvManageVo;

@Service("envManageService")
public class EnvManageServiceImpl implements EnvManageService {

	@Autowired
	private EnvManageDAO envManageDAO;

	@Autowired
	private SupplierService supplierService;

	@Autowired
	private EnvManageValidator envManageValidator;

	@Override
	public DataGrid<EnvManageVo> getDataGridVoBy(EnvManage envManage, Pager pager) {

		return envManageDAO.getDataGridVoBy(envManage, pager);
	}

	@Override
	public int delete(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<EnvManage> findList(EnvManage entity) {
		EnvManageExample example = new EnvManageExample();
		EnvManageExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		return envManageDAO.selectEnvManageByExample(example);
	}

	@Override
	public EnvManage get(Long id) {
		// TODO Auto-generated method stub
		return envManageDAO.selectEnvManageByPrimaryKey(id);
	}

	@Override
	public DataGrid<EnvManage> getDataGrid(EnvManage entity, Pager pager) {

		return null;
	}

	@Override
	public Long insert(EnvManage entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(EnvManage entity) {
		// TODO Auto-generated method stub
		return envManageDAO.updateEnvManageByPrimaryKey(entity);
	}

	@Override
	public void deleteEnvManage(List<Long> ids) {

		if (ids != null && !ids.isEmpty()) {
			for (int i = 0; i < ids.size(); i++) {
				EnvManage envManage = envManageDAO.selectEnvManageByPrimaryKey(ids.get(i));
				envManage.setState(ConstantSqm.STATUS_DELETE);
				envManageDAO.updateEnvManageByPrimaryKey(envManage);
			}
		}

	}

	@Override
	public void txAddEnvManage(String envManage, ReturnMessage message, BindingResult result) throws Exception {
		EnvManageVo vo = new EnvManageVo();
		Map<String, String> map = null;
		if (StringUtil.isNotBlank(envManage)) {
			map = JsonUtil.json2Bean(envManage, new TypeToken<Map<String, String>>() {
			});
		}
		BeanUtils.populate(vo, map);

		EnvManage bean = new EnvManage();
		BeanUtils.copyProperties(bean, vo);

		Supplier supplier = supplierService.get(Long.valueOf(vo.getSupplierId()));
		vo.setSupplierCode(supplier.getCode());
		envManageValidator.methodAddValidate(vo, result);
		if (result.hasErrors()) {
			message.setObj(envManageValidator.getErrors(result));
		} else {
			EnvManage pojo=new EnvManage();
			pojo.setSupplierId(vo.getSupplierId());
			List<EnvManage> list =findListByStateDelete(pojo);
			for (EnvManage envManage2 : list) {
				envManage2.setComment(null);
				envManage2.setValue(null);
				envManage2.setExpiredDate(null);
				envManage2.setUpdateRequired(null);
				envManage2.setCertificationDate(null);
				envManage2.setCertificationType(null);
				envManageDAO.updateEnvManageByPrimaryKey(envManage2);
			}
			
			if (!StringUtil.isBlank(vo.getCertificationDates())) {
				bean.setCertificationDate(DateUtil.string2date(vo.getCertificationDates(), DateUtil.DATE_FORMAT));
			}
			if (!StringUtil.isBlank(vo.getExpiredDates())) {
				bean.setExpiredDate(DateUtil.string2date(vo.getExpiredDates(), DateUtil.DATE_FORMAT));
			}
			bean.setSupplierCode(supplier.getCode());
			bean.setSupplierName(supplier.getName());
			bean.setState(ConstantSqm.STATUS_NORMAL);
			
			List<EnvManage> obj=findListByStateDelete(bean);
			if(obj!=null&&obj.size()>0){
				EnvManage envManageobj=obj.get(0);
				envManageobj.setComment(bean.getComment());
				envManageobj.setValue(bean.getValue());
				if(bean.getExpiredDate()!=null)
				envManageobj.setExpiredDate(bean.getExpiredDate());
				envManageobj.setUpdateRequired(bean.getUpdateRequired());
				if(bean.getCertificationDate()!=null)
				envManageobj.setCertificationDate(bean.getCertificationDate());
				envManageobj.setCertificationType(bean.getCertificationType());
				envManageobj.setState(ConstantSqm.STATUS_NORMAL);
				envManageDAO.updateEnvManageByPrimaryKey(envManageobj);
			}else{
				envManageDAO.insertEnvManageSelective(bean);
			}
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);

		}

	}

	@Override
	public EnvManageVo getModifyData(Long id) throws Exception {
		EnvManageVo vo = new EnvManageVo();
		EnvManage bean = envManageDAO.selectEnvManageByPrimaryKey(id);
		BeanUtils.copyProperties(vo, bean);
		vo.setCertificationDates(DateUtil.date2string(bean.getCertificationDate(), DateUtil.DATE_FORMAT));
		vo.setExpiredDates(DateUtil.date2string(bean.getExpiredDate(), DateUtil.DATE_FORMAT));
		return vo;
	}

	@Override
	public void txUpdateOrAddEnvManage(String envManage) throws Exception {

		EnvManageVo vo = new EnvManageVo();
		Map<String, String> map = null;
		if (StringUtil.isNotBlank(envManage)) {
			map = JsonUtil.json2Bean(envManage, new TypeToken<Map<String, String>>() {
			});
		}
		BeanUtils.populate(vo, map);
		EnvManage bean = envManageDAO.selectEnvManageByPrimaryKey(vo.getId());
		Supplier supplier = supplierService.get(Long.valueOf(vo.getSupplierId()));

		bean.setComment(vo.getComment());
		bean.setSupplierId(vo.getSupplierId());
		bean.setSupplierCode(supplier.getCode());
		bean.setSupplierName(supplier.getName());
		bean.setValue(vo.getValue());
		bean.setCertificationType(vo.getCertificationType());
		
		if (!StringUtil.isBlank(vo.getComment())) {
			bean.setComment(vo.getComment());
		} else {
			bean.setComment(null);
		} 
		
		if (!StringUtil.isBlank(vo.getUpdateRequired())) {
			bean.setUpdateRequired(vo.getUpdateRequired());
		} else {
			bean.setUpdateRequired(null);
		}

		if (!StringUtil.isBlank(vo.getCertificationDates())) {
			bean.setCertificationDate(DateUtil.string2date(vo.getCertificationDates(), DateUtil.DATE_FORMAT));
		} else {
			bean.setCertificationDate(null);
		}
		if (!StringUtil.isBlank(vo.getExpiredDates())) {
			bean.setExpiredDate(DateUtil.string2date(vo.getExpiredDates(), DateUtil.DATE_FORMAT));
		} else {
			bean.setExpiredDate(null);
		}
		envManageDAO.updateEnvManageByPrimaryKey(bean);
	}
	/**
	 * 查询所有
	 */
	@Override
	public List<EnvManage> queryAll() {
		EnvManageExample example = new EnvManageExample();
		List<EnvManage> supplierList = envManageDAO.selectEnvManageByExample(example);
		return supplierList;
	}

	public List<EnvManage> findListByStateDelete(EnvManage entity) {
		EnvManageExample example = new EnvManageExample();
		EnvManageExample.Criteria criteria = example.createCriteria();
		criteria.andStateEqualTo(ConstantSqm.STATUS_DELETE);
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		return envManageDAO.selectEnvManageByExample(example);
	}
}
